//LSL-script

//#########################################################################
//        MYrss - Logo
//
// (C) Copyright by Hachreak
//       Released under GPL v2.
//       Author : Hachreak (hachreak at gmail dot com)
//       WebSite: www.secondlifelab.it
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
//
//#########################################################################

// Numero di notizie pubblicate alla volta.
//integer NUMBER = 5;

// Lo script si resetta e quindi aggiorna le notizie
// ogni 1500 secondi (25 minuti)
integer UPDATE_TIME = 1500;

// When click into the object
string PROMO_MSG = "Vai al sito di  SecondLifeLab.it (SLLAB)";

string GO_ON_CLICK = "http://www.secondlifelab.it";

key key_detected;

//integer verion_request = FALSE;

default{

    state_entry(){
	    // Set timer for update
            llSetTimerEvent(UPDATE_TIME);
	    // it makes to turn the logo
            llTargetOmega(<0,0,1>,PI,1.0);
	    // Send a msg at all
            //llSay(0, "Aggiornamento dati RSS eseguito");
            // Send a msg to root for request the list of title
            llMessageLinked(LINK_ROOT, llGetLinkNumber(), "", NULL_KEY);

	    // azzero la chiave
//	    verion_request = FALSE;
    }

    touch_start(integer total_number){
	// aggiorna sull'ultima versione disponibile
//	key_detected = llDetectedKey(0);
//	verion_request = TRUE;
//	llMessageLinked(LINK_ROOT, llGetLinkNumber(), "version", NULL_KEY);
	state request_last_version;
    }

    // sender_num is the link number of the sending object in the set.
    // num, str, and id are arbitrary values passed to the llMessageLinked function.
    link_message(integer sender_num, integer num, string msg, key id) {        
        
//	if(verion_request == FALSE){

	if(num == llGetLinkNumber()){
//		if(verion_request == FALSE){
			// The root send the news titles
        	        list    out  = llParseString2List(msg,["\n"],[]);
	        	integer i    = 0;
	        	string  text = "";
                
			for(i=0; i<llGetListLength(out); i++){
	        		text += llList2String(out,i)+"\n";
	        	}

			if(llStringLength(text) > 254){
				text = llGetSubString(text,0,251)+"..";
			}

        	        // visualizzo i titoli aggiornati
                	llSetText(text,<0.4, 0.8, 1.0>, 1);
			// avverto che ho aggiornato
			llSay(0, "Aggiornamento dati RSS eseguito");
//		}else{
//			verion_request = FALSE;
//		}
	}else if(num == 0){
		// Comando di reset per lo script
		llResetScript();
	}
//	}else{
//		llInstantMessage(key_detected, msg);
//		verion_request = FALSE;
//	}
    }

    timer(){
        llResetScript();
    }

}

state request_last_version{
	state_entry(){
		// richiedo quale e' l'ultima versione
		llMessageLinked(LINK_ROOT, llGetLinkNumber(), "version", NULL_KEY);
		// timer di timeout (30 secondi)
		llSetTimerEvent(30);
	}

	link_message(integer sender_num, integer num, string msg, key id){
		// guardo se e' x me il msg
		if(num == llGetLinkNumber()){
			// lo stampo in chat pubblica
			llSay(0,msg);
			llResetScript();
		}		
	}

	timer(){
		llResetScript();
	}
}
